package com.marsrover.command;

import static com.marsrover.Directions.*;

import com.marsrover.Location;
import com.marsrover.LocationFormatException;
import com.marsrover.Plateau;

public class MoveCommand implements Command {
	private Location location;
	private Plateau plateau;

	public MoveCommand(Location location, Plateau plateau) {
		this.location = location;
		this.plateau = plateau;
	}

	@Override
	public void execute() {
		int newX;
		int newY;
		switch (location.getDirection()) {
			case NORTH:
				newX = location.getX();
				newY = location.getY() + 1;
				break;
			case SOUTH:
				newX = location.getX();
				newY = location.getY() - 1;
				break;
			case EAST:
				newX = location.getX() + 1;
				newY = location.getY();
				break;
			case WEST:
				newX = location.getX() - 1;
				newY = location.getY();
				break;
			default:
				throw new LocationFormatException();
		}

		if (plateau.isInside(newX, newY)) {
			location.setX(newX);
			location.setY(newY);
		}
	}
}
